Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

with_param #159

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

with_param #159

wants to merge 2 commits into from

Conversation

serprex
Copy link
Member

@serprex serprex commented Sep 24, 2024

Small wrapper around with_option for impl Serialize + Bind

@serprex serprex force-pushed the with-param branch 2 times, most recently from 8891fb7 to 32382c9 Compare September 24, 2024 13:46
@serprex serprex changed the title With param with_param Sep 24, 2024
@serprex serprex force-pushed the with-param branch 2 times, most recently from edbd788 to abf96dd Compare September 24, 2024 13:55
@slvrtrn
Copy link
Contributor

slvrtrn commented Sep 24, 2024

Aside from the simpler types, the following needs testing:

  • Arrays, Maps, Tuples (e.g., common container types that make sense to use in this context).
  • DateTime/DateTime64 (IIRC, it requires a specific format like 2022-05-02 13:25:55.123456789, i.e. not exactly ISO timestamp)
  • Numeric and named enums
  • \t, \r, \n, single quotes, backslashes in the bound strings
  • Strings with UTF8 symbols
  • Nulls

I'd maybe also not mix it with .with_option method; the proposal from @mshustov looks better IMO (see this comment) - we can add the param_ prefix ourselves internally.

@serprex serprex force-pushed the with-param branch 2 times, most recently from 135598b to 70aa220 Compare September 24, 2024 14:35
@serprex
Copy link
Member Author

serprex commented Sep 24, 2024

So already found issue with basic tests. String Bind serializes to 'string' with quotes which is correct when interpolating into query string, but for server side parameters the string should be unquoted

Note once a string is contained it is expected to be quoted in params: ClickHouse/clickhouse-connect#159

@serprex serprex force-pushed the with-param branch 4 times, most recently from 5466a0e to 19ba376 Compare September 26, 2024 02:29
src/lib.rs Outdated
@@ -160,6 +161,12 @@ impl Client {
self
}

pub fn with_param(self, name: &str, value: impl Serialize) -> Result<Self, String> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of Client::with_param? Parameters are specific for queries, not the whole client

Copy link
Member Author

@serprex serprex Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, but if someone wants to share parameter between queries queries they can put it on the client

For example, in a multi tenant app, tenantid could be put as a parameter in the function creating client

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, no other client allows to set the global parameters for all queries in the instance configuration; it can also potentially lead to some obscure outcomes.
IMO, I'd maybe remove the client-level binding for now, leaving it only for queries, and merge this PR.

@serprex @loyd @mshustov

Copy link
Member Author

@serprex serprex Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the obscure outcomes you're thinking of? Better people use this than roll it themselves with options

This keeps things simple compared to https://github.com/citusdata/django-multitenant or https://github.com/citusdata/activerecord-multi-tenant tho those hook into ORM to inject predicates

@loyd
Copy link
Collaborator

loyd commented Sep 29, 2024

We also need tests for UUID (probably, with the human-readable mode it works already). Also, for some implementation of DecimalX (the fixnum crate for instance).

And need to add some examples.

src/query.rs Outdated Show resolved Hide resolved
src/query.rs Outdated Show resolved Hide resolved
src/query.rs Outdated
@@ -195,6 +195,12 @@ impl Query {
self.client.add_option(name, value);
self
}

pub fn with_param(self, name: &str, value: impl Serialize) -> Result<Self, String> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method must be well documented. Also, we should specify the difference between param() and bind() (probably in a dedicated section on Query and provide links to that section in docs of both methods).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added some docs, signature should somewhat communicate difference given ? is positional while server side parameters are key/value

src/query.rs Outdated Show resolved Hide resolved
@serprex serprex force-pushed the with-param branch 4 times, most recently from 2eaaa70 to 759a7e0 Compare October 2, 2024 18:04
@mshustov mshustov requested a review from loyd October 29, 2024 18:12
@slvrtrn
Copy link
Contributor

slvrtrn commented Nov 4, 2024

@loyd, I'd like to merge this one. WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants